package com.niiwoo.civet.trade.report.upload.security;


import com.niiwoo.civet.trade.report.upload.exception.Base64Exception;
import com.niiwoo.civet.trade.report.upload.exception.DecryptDataException;
import com.niiwoo.civet.trade.report.upload.exception.EncryptDataException;
import com.niiwoo.civet.trade.report.upload.exception.HashException;
import com.niiwoo.civet.trade.report.upload.util.Base64Util;
import com.niiwoo.civet.trade.report.upload.util.FileUtil;
import com.niiwoo.civet.trade.report.upload.util.HashUtil;

/**
 * 
 * @author wanglei02
 *
 */
public class SecurityEngineClient {
	 
	public String hashFile(String inFile)  throws HashException {
		
		HashUtil hashUtil = new HashUtil();
		byte[] hashData = hashUtil.BJS_HashFile(inFile);
		
		Base64Util base64Util = new Base64Util();
		
		String hashValue = base64Util.BJS_EncodeBase64(hashData);
		
		return hashValue;
	}
	
	/**
	 *
	 * @param key
	 * @param inFile
	 * @param outFile
	 * @param encryptMethod
	 * @return
	 * @throws Base64Exception
	 * @throws DecryptDataException
	 */
	public boolean decryptFile(String key, String inFile) throws Base64Exception, DecryptDataException
	{
		String outFile = inFile.replaceAll(".cipher", "");
		Base64Util base64Util = new Base64Util();
		FileUtil fileUtil = new FileUtil();
		
		byte[] keybuf = base64Util.BJS_DecodeBase64(key);
		boolean rv = fileUtil.BJS_SymmDecFile(inFile, outFile, keybuf,1L);
		return rv;
	}
	
	/**
	 *
	 * @param key
	 * @param inFile
	 * @param outFile
	 * @param encryptMethod
	 * @return
	 * @throws Base64Exception
	 * @throws EncryptDataException
	 */
	public boolean encryptFile(String key, String inFile) throws Base64Exception, EncryptDataException
	{
		String outFile = inFile+".cipher";
		Base64Util base64Util = new Base64Util();
		FileUtil fileUtil = new FileUtil();
		
		byte[] keybuf = base64Util.BJS_DecodeBase64(key);
		boolean rv = fileUtil.BJS_SymmEncFile(inFile, outFile, keybuf,1L);
		return rv;
	}
  
}